﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jalal.Library.Data;
using System.Data;
using System.Web;
using System.Web.Caching;
using BCGC.DAL;
using System.Data.SqlTypes;
using Jalal.Library.Utility;
using Jalal.Library.DataStructure;

namespace BCGC.BLL.BusinessObject
{
    public class EmailTemplate
    {
        private int id;
        public int Id
        {
            get { return id; }
        }

        private string mailTitle;
        public string MailTitle
        {
            get { return mailTitle; }
            set { mailTitle = value; }
        }

        private string mailSubject;
        public string MailSubject
        {
            get { return mailSubject; }
            set { mailSubject = value; }
        }

        private string mailBody;
        public string MailBody
        {
            get { return mailBody; }
            set { mailBody = value; }
        }

        private DateTime createDateTime;
        public DateTime CreateDateTime
        {
            get { return createDateTime; }
            set { createDateTime = value; }
        }

        private DateTime lastUpdateDateTime;
        public DateTime LastUpdateDateTime
        {
            get { return lastUpdateDateTime; }
            set { lastUpdateDateTime = value; }
        }

        public EmailTemplate()
        {
            this.id = 0;
            this.createDateTime = SqlDateTime.MinValue.Value;
            this.lastUpdateDateTime = SqlDateTime.MinValue.Value;
            this.mailBody = string.Empty;
            this.mailSubject = string.Empty;
            this.mailTitle = string.Empty;
        }

        public EmailTemplate(DataRow dataRow)
        {
            if (dataRow != null)
            {
                if (dataRow.Table.Columns.Contains("Id"))
                    this.id = (int)CommonUtility.FilterNull(dataRow["Id"], typeof(int));
                if (dataRow.Table.Columns.Contains("CreateDateTime"))
                    this.createDateTime = (DateTime)CommonUtility.FilterNull(dataRow["CreateDateTime"], typeof(DateTime));
                if (dataRow.Table.Columns.Contains("LastUpdateDateTime"))
                    this.lastUpdateDateTime = (DateTime)CommonUtility.FilterNull(dataRow["LastUpdateDateTime"], typeof(DateTime));
                if (dataRow.Table.Columns.Contains("MailBody"))
                    this.mailBody = (string)CommonUtility.FilterNull(dataRow["MailBody"], typeof(string));
                if (dataRow.Table.Columns.Contains("MailSubject"))
                    this.mailSubject = (string)CommonUtility.FilterNull(dataRow["MailSubject"], typeof(string));
                if (dataRow.Table.Columns.Contains("MailTitle"))
                    this.mailTitle = (string)CommonUtility.FilterNull(dataRow["MailTitle"], typeof(string));
            }
            else
            {
                this.id = 0;
                this.createDateTime = SqlDateTime.MinValue.Value;
                this.lastUpdateDateTime = SqlDateTime.MinValue.Value;
                this.mailBody = string.Empty;
                this.mailSubject = string.Empty;
                this.mailTitle = string.Empty;
            }
        }

        private static ReadWriteCustomCollection<EmailTemplate> LoadFromDataTable(DataTable data)
        {
            ReadWriteCustomCollection<EmailTemplate> result = new ReadWriteCustomCollection<EmailTemplate>();

            if (data != null && data.Rows.Count > 0)
            {
                foreach (DataRow row in data.Rows)
                {
                    result.Add(new EmailTemplate(row));
                }
            }
            return result;
        }

        public static EmailTemplate GetEmailTemplateByName(string templateName)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            EmailTemplate template = null;
            DataTable data = EmailTemplateData.GetTemplateByTitle(templateName);
            if (data != null && data.Rows.Count > 0)
                template = new EmailTemplate(data.Rows[0]);

            DataFactory.GetDataMiner().CommitTransaction();

            return template;
        }

        public static ReadWriteCustomCollection<EmailTemplate> GetTemplateList()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            ReadWriteCustomCollection<EmailTemplate> templates = null;
            DataTable data = EmailTemplateData.GetTemplateList();
            if (data != null && data.Rows.Count > 0)
                templates = LoadFromDataTable(data);

            DataFactory.GetDataMiner().CommitTransaction();

            return templates;
        }

        public static bool Create(string title, string subject, string body)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int id = EmailTemplateData.CreateNewEmailTemplate(title, subject, body);
            DataFactory.GetDataMiner().CommitTransaction();

            return (id > 0);
        }

        public static bool UpdateEmailTemplate(int templateId, string title, string subject, string body)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int updateCount = EmailTemplateData.UpdateEmailTemplate(templateId, title, subject, body);
            DataFactory.GetDataMiner().CommitTransaction();

            return (updateCount > 0);
        }

        public static bool TemplateAlreadyExists(string title)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int itemCount = EmailTemplateData.CountTemplateByTitle(title);
            DataFactory.GetDataMiner().CommitTransaction();

            return (itemCount > 0);
        }

        public static EmailTemplate GetEmailTemplateById(int id)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            EmailTemplate template = null;
            DataTable data = EmailTemplateData.GetTemplateById(id);
            if (data != null && data.Rows.Count > 0)
                template = new EmailTemplate(data.Rows[0]);

            DataFactory.GetDataMiner().CommitTransaction();

            return template;
        }

        public static void Delete(List<int> templatesToDelete)
        {
            DataFactory.GetDataMiner().BeginTransaction(false);

            try
            {
                int deletedItemCount = EmailTemplateData.DeleteTemplates(templatesToDelete);
                if (deletedItemCount == templatesToDelete.Count)
                    DataFactory.GetDataMiner().CommitTransaction();
                else
                    throw new Exception("Failed to delete all items");
            }
            catch (Exception ex)
            {
                DataFactory.GetDataMiner().RollBackTransaction();
                throw ex;
            }
        }
    }
}
